const app = getApp();
import { hbappRequest } from "/utils/requests";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        showContact: true,
        suspendShow: true,
        showIcon: true,
        bgcolor: '',
        form: true,
        table: false,
        options: {
            s1: 'y',
            xiangmu: "sk",
            lx: "sk",
            selectNo: 'y',
        },
        yl: 1,
        gonghao: '无',
        fenxiao: '无',
        laiyuan: '检索职位',
        chaxunjg: '',
        cxlists: [],
        showIcon: !0,
        isShowCdNav: !1,
        onReachBottomNum: 0,
        listId: 0,
        lists: [{
            'title': '按职位信息查询',
            "zt": 'on'
        }, {
            'title': '按个人条件查询',
            "zt": ''
        }],
        majorvalue: "",
        nav: 1,
        zwid: 12,
        actid: 17122,
        name: '',
        title: '',
        xueli: true,
        zhuanye: true,
        cxlists: [],
        filed: {},
        file: [],
        xueliArr: [{
            id: "3_1",
            text: "高中"
        }, {
            id: "3_2",
            text: "专科"
        }, {
            id: "3_3",
            text: "本科"
        }, {
            id: "3_4",
            text: "研究生"
        }],
        bklx: [{
            id: "5_1",
            text: "综合岗"
        }, {
            id: "5_2",
            text: "教育岗"
        }, {
            id: "5_3",
            text: "医疗岗"
        }],
        bklxvalue: '',
        xueli: true,
        zhuanye: true,
        xiangmuok: false,
        zhunkaozhengok: false,
        zhunkaozheng: '',
        wx: '',
        myPhone: '',
        isSydw: false
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        this.setData(await app.methods.myPhone()); // 获取本地存储手机号
        if(!this.data.myPhone) {
            await app.methods.getPhoneNumber()
            this.setData({
              isShowAuth: !0
            })
        }
        xhs.showShareMenu({
          shareTypes: 'normal',
        })
        let links, jxOptions;
        if (options.scene) {
            options.scene = options.scene.replace(/%253D/g, ".");
            options.scene = options.scene.replace(/%2526/g, "-");
          jxOptions = await app.methods.getOPtions(options);
        }
        let id = options.scene ? jxOptions.id : options.id;
        options.id = id;
        links = await app.methods.getLink(id);
        options.fx = options.scene ? jxOptions.fx : options.fx;
        options.gh = options.scene ? jxOptions.gh : options.gh;
        options.zwid = links.zwid ? links.zwid : '';
        options.lx = links.lx ? links.lx : '';
        options.yl = links.yl ? links.yl : '';
        options.s1 = links.s1 ? links.s1 : 'y';
        options.selectNo = links.selectNo ? links.selectNo : 'y';
        options.actid = Number(links.actid) ? Number(links.actid) : undefined;
        // console.log(options)
        if (options.yl != undefined) {
            if (options.yl.length > 0) {
                this.setData({
                    yl: Number(options.yl)
                })
            }
        }
        if (options.lx != undefined) {
            if (options.lx.length > 0) {
                this.setData({
                    isSydw: true
                })
                if (options.lx == 'jztg') {
                    this.setData({
                        bklx: [{
                            id: "5_1",
                            text: "教师招聘"
                        }, {
                            id: "5_2",
                            text: "特岗教师"
                        }]
                    })
                }
            }
        }
        if (options.actid != undefined) {
            this.setData({
                actid: Number(options.actid)
            })
        }
        this.setData({
            options: options,
            actid: Number(options.actid) ? Number(options.actid) : undefined
        })
        this.setData(await app.methods.getIdentity(options)); // 获取个人身份信息
        if (this.data.getIdentity.crm) {
          options.wxh = this.data.getIdentity.crm
        } else {
          options.wxh = options.gh
        }
        this.setData(await app.methods.getWechat(options)); // 获取个人身份信息
        this.setData(await app.methods.guishudi(options)); // 获取归属地
        let fenbu = this.data.fenbu;
        let fenxiao = this.data.fenxiao;
        let getIdentity = this.data.getIdentity;
        getIdentity.fenbu = fenbu;
        getIdentity.fenxiao = fenxiao;
        getIdentity.zzname = this.data.zzname;
        getIdentity.zzid = this.data.zzid;
        getIdentity.erpdqm = this.data.erpdqm;
        this.setData({
          getIdentity: getIdentity
        })
        await this.getHD();
        await this.levelOne();
        this.data.myPhone == '' ? '' : this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'))
        // console.log(this.data)
        this.thisMethod()
    },
    m_select_touch: async function (t) {
        // console.log(t)
        var e = t.detail.selIndex,
            a = t.detail.stype,
            c = t.detail.level,
            f = t.detail.selectText;
        if (c == 1) {
            let data = {
                level: '2',
                'grfiled': 'str2',
                onefiled: 'str1',
                onetext: f == '不限' ? '' : f
            }
            let level2 = await this.level(data, c, 'str2', this.data.listId)
            a == '1' ? this.setData({
                str2: level2,
                str1value: t.detail.selectText
            }) : this.setData({
                str22: level2,
                str11value: t.detail.selectText == "不限" ? '' : t.detail.selectText
            })
            // console.log(this.data.str1value,this.data.str2,this.data.str11value,this.data.str22)
        } else if (c == 2) {
            let data = {
                level: '3',
                'grfiled': 'str3',
                onefiled: 'str1',
                onetext: a == '2' ? this.data.str1value : this.data.str11value,
                twofiled: 'str2',
                twotext: f
            }
            let level3 = await this.level(data, c, 'str3', this.data.listId)
            a == '2' ? this.setData({
                str3: level3,
                str2value: t.detail.selectText
            }) : this.setData({
                str33: level3,
                str22value: t.detail.selectText == "不限" ? '' : t.detail.selectText
            })
            // console.log(this.data.str2value,this.data.str3,this.data.str22value,this.data.str33)
        } else if (c == 3) {
            let data = {
                level: '4',
                'grfiled': 'str4',
                onefiled: 'str1',
                onetext: a == '3' ? this.data.str1value : this.data.str11value,
                twofiled: 'str2',
                twotext: a == '3' ? this.data.str2value : this.data.str22value,
                threefiled: 'str3',
                threetext: f
            }
            let level4 = await this.level(data, c, 'str4', this.data.listId)
            a == '3' ? this.setData({
                str4: level4,
                str3value: t.detail.selectText
            }) : this.setData({
                str44: level4,
                xueliArrvalue: f
            })
            // console.log(this.data.str3value,this.data.str4,this.data.xueliArrvalue,this.data.str44)
        } else if (c == 4) {
            a == '4' ? this.setData({
                str4value: t.detail.selectText
            }) : this.setData({
                str33value: t.detail.selectText == "不限" ? '' : t.detail.selectText
            })
        } else if (c == 5) {
            this.setData({
                bklxvalue: f
            })
        }
    },
    // 获取活动信息
    async getHD() {
        return new Promise((resolve) => {
            hbappRequest({
                url: '/hbqt/web/index.php/Skzw/zwsearch/zhiwei_info?callback=&',
                method: 'GET',
                dataType: 'jsonp',
                data: {
                    zwid: this.data.options.zwid
                },
            }).then(res => {
                let newStr = app.methods.getAPIReturn(res);
                // console.log(newStr)
                if (newStr.status == 1) {
                    let field = []
                    for (let i = 0; i < newStr.data.lists.length; i++) {
                        field.push(newStr.data.lists[i].field)
                    }
                    if (field.indexOf('xueli') < 0) {
                        this.setData({
                            xueli: false
                        })
                    }
                    if (field.indexOf('zhuanye') < 0) {
                        this.setData({
                            zhuanye: true
                        })
                    }
                    this.setData({
                        name: app.methods.htmlDecodeByRegExp(newStr.data.name),
                        name1: app.methods.htmlDecodeByRegExp(newStr.data.name),
                        title: app.methods.htmlDecodeByRegExp(newStr.data.title),
                        filed: newStr.data.lists,
                        actid: Number(this.data.options.actid) ? Number(this.data.options.actid) : Number(newStr.data.actid),
                        nav: Number(newStr.data.nav),
                        ms: Number(newStr.data.ms),
                        k_name: newStr.data.k_name,
                        k_url: newStr.data.k_url,
                    })
                    app.globalData.laiyuan = this.data.name
                    if (Number(newStr.data.nav) != undefined) {
                        if (Number(newStr.data.nav) == 0) {
                            this.setData({
                                lists: false
                            })
                        } else if (Number(newStr.data.nav) == 2) {
                            this.setData({
                                lists: false,
                                listId: 1
                            })
                        }
                    }
                }
                resolve()
            }).catch(err => {
                console.log(err)
                resolve()
            })
        })
    },
    // 获取是否跳转
    async IsJump(zwid) {
        return new Promise((resolve) => {
            DB.collection('zhiwei').where({
                'zwid': zwid,
                'jump': true
            }).get().then((res) => {
                if (res.data.length > 0) {
                    xhs.redirectTo({
                        url: res.data[0].link + '&gh=' + this.data.options.gh + '&fx=' + this.data.options.fx + '&wxh=' + this.data.options.wxh
                    });
                }
                resolve(res)
            })
        })
    },
    async levelOne() {
        return new Promise((resolve) => {
            this.data.options.selectNo == 'y' ? hbappRequest({
                url: '/hbqt/web/index.php/Skzw/zwsearch/getlevel_list?callback=&',
                method: 'GET',
                dataType: 'jsonp',
                data: {
                    zwid: this.data.options.zwid,
                    level: '1',
                    'grfiled': 'str1'
                },
            }).then(res => {
                let newStr = app.methods.getAPIReturn(res);
                // console.log(newStr)
                var newArr = [];
                var newArr2 = [];
                if (newStr.status == 1) {
                    var newObj1 = {}
                    newObj1.id = '1_' + 0;
                    newObj1.text = '不限';
                    newArr.push(newObj1)
                    for (var i = 1; i <= newStr.data.length; i++) {
                        var newObj = {};
                        var newObj2 = {};
                        newObj.id = '1_' + i;
                        newObj.text = newStr.data[i - 1].str1;
                        newArr.push(newObj)
                        newObj2.id = '1_' + (i - 1);
                        newObj2.text = newStr.data[i - 1].str1;
                        newArr2.push(newObj2)
                    }
                    this.setData({
                        str11: newArr,
                        str1: newArr2,
                        ms: newStr.data.ms
                    })
                }
            }).catch(err => {
                console.log(err)
            }) : this.setData({
                str11: [],
                str1: [],
            })
            this.data.ms == 0 ? this.setData({
                zhunkaozhengok: true
            }) : this.setData({
                zhunkaozhengok: false
            })
            resolve()
        })
    },
    // 选择专业
    m_selectSearch_touch: function (t) {
        var e = t.detail.selText;
        // console.log(e)
        if (t.currentTarget.dataset.str11) {
            this.setData({
                str11value: e
            });
        } else {
            this.setData({
                majorvalue: e
            });
        }
    },
    // 切换“按个人条件查询”与“按职位信息查询”
    recoveryState: function (e) {
        if (e.currentTarget.id == 'on') {
            // console.log('不变')
        } else {
            if (e.currentTarget.dataset.num == 0) {
                this.setData({
                    listId: 0,
                    lists: [{
                        'title': '按职位信息查询',
                        "zt": 'on'
                    }, {
                        'title': '按个人条件查询',
                        "zt": ''
                    }]
                })
            } else if (e.currentTarget.dataset.num == 1) {
                this.setData({
                    listId: 1,
                    lists: [{
                        'title': '按职位信息查询',
                        "zt": ''
                    }, {
                        'title': '按个人条件查询',
                        "zt": 'on'
                    }]
                })
            }
        }
        // console.log(this)
        // console.log(e)
    },
    // 级联获取信息
    async level(data, level, levels, listId) {
        return new Promise((resolve) => {
            hbappRequest({
                url: '/hbqt/web/index.php/Skzw/zwsearch/getlevel_list?callback=&',
                type: 'GET',
                dataType: 'jsonp',
                data: {
                    ...data,
                    zwid: this.data.options.zwid
                },
            }).then(res => {
                let newStr = app.methods.getAPIReturn(res);
                // console.log(levels)
                var newArr = [];
                if (listId == 1) {
                    var newObj1 = {}
                    newObj1.id = level + '_' + 0;
                    newObj1.text = '不限';
                    newArr.push(newObj1)
                }
                for (var i = (listId == 0 ? 0 : 1); i < (listId == 0 ? newStr.data.length : newStr.data.length + 1); i++) {
                    var newObj = {};
                    newObj.id = level + '_' + i;
                    let j = (listId == 0 ? i : i - 1)
                    newObj.text = newStr.data[j][levels];
                    newArr.push(newObj)
                }
                resolve(newArr)
            })
        }).catch(err => {
            console.log(err)
            resolve()
        })
    },
    // 按职位信息查询
    async clickfn1() {
        if(!this.data.myPhone) {
          this.setData({
            isShowAuth: !0,
          })
          return false
        }
        if (this.data.str1value == '') {
            xhs.showToast({
                title: '请选择第一项',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if ((this.data.filed[1].name) && (!this.data.str2value)) {
            xhs.showToast({
                title: '请选择第二项',
                icon: 'none',
                duration: 2000
            })
            return false
        } else if ((!this.data.filed[1].name) && (!this.data.str2value)) {
            this.setData({
                str2value: ''
            })
        }
        if (this.data.zhunkaozhengok && !this.data.zhunkaozheng) {
            xhs.showToast({
                title: '请输入准考证号',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (!this.data.str3value) {
            this.setData({
                str3value: ''
            })
        }
        if (!this.data.str4value) {
            this.setData({
                str4value: ''
            })
        }
        if ((this.data.xiangmuok) && (this.data.isSydw) && (this.data.bklxvalue == '')) {
            xhs.showToast({
                title: '请选择备考类型',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        // console.log(this.data.str1value, this.data.str2value, this.data.str3value, this.data.str4value)
        let data = {
            str1: this.data.str1value,
            str2: this.data.str2value,
            str3: this.data.str3value,
            str4: this.data.str4value,
        }
        await this.chaxun(data)
        // console.log(this.data.getIdentity)
        this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.actid, 'chaxun'))
        if (!this.data.user) {
            await app.methods.registerCX(this.data.myPhone, this.data.actid, this.data.name1, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, this.data.zhunkaozheng, '', 'chaxun')
        } else {
            await app.methods.updateuserCX(this.data.myPhone, this.data.actid, this.data.name1, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, this.data.zhunkaozheng, 'chaxun', '', this.data.user.user)
        }
        // await app.methods.registerChaxun(this.data.myPhone, this.data.actid, this.data.name1, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, this.data.zhunkaozheng)
        this.setData({
            xiangmuok: false
        })
        this.setData({
            zhunkaozhengok: false
        })
    },
    // 按个人条件查询
    async clickfn() {
        if(!this.data.myPhone) {
          this.setData({
            isShowAuth: !0,
          })
          return false
        }
        if (!this.data.str22value) {
            this.setData({
                str22value: ''
            })
        }
        if (!this.data.str33value) {
            this.setData({
                str33value: ''
            })
        }
        if (!this.data.xueliArrvalue) {
            this.setData({
                xueliArrvalue: ''
            })
        }
        if ((!this.data.majorvalue) || (this.data.majorvalue == '不限')) {
            this.setData({
                majorvalue: ''
            })
        }
        if ((this.data.xiangmuok) && (this.data.options.lx == 'sydw' || this.data.options.lx == 'jztg') && (this.data.bklxvalue == '')) {
            xhs.showToast({
                title: '请选择备考类型',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        // console.log(this.data.str11value, this.data.str22value, this.data.str33value, this.data.xueliArrvalue, this.data.majorvalue)
        let data = {
            str1: this.data.str11value,
            str2: this.data.str22value,
            str3: this.data.str33value,
            xueli: this.data.xueliArrvalue,
            zhuanye: this.data.majorvalue,
        }
        await this.chaxun(data)
        // console.log(this.data.getIdentity)
        this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.actid, 'chaxun'))
        // console.log(this.data)
        if (!this.data.user) {
            await app.methods.registerCX(this.data.myPhone, this.data.actid, this.data.name1, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, this.data.zhunkaozheng, '', 'chaxun')
        } else {
            await app.methods.updateuserCX(this.data.myPhone, this.data.actid, this.data.name1, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, this.data.zhunkaozheng, 'chaxun', '', this.data.user.user)
        }
        // await app.methods.registerChaxun(this.data.myPhone, this.data.actid, this.data.name1, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, this.data.zhunkaozheng)
        this.setData({
            xiangmuok: false
        })
        this.setData({
            zhunkaozhengok: false
        })
    },
    // 查询数据
    async chaxun(data) {
        return new Promise((resolve) => {
            hbappRequest({
                url: '/hbqt/web/index.php/Skzw/zwsearch/getsearch_list?callback=&',
                type: 'GET',
                dataType: 'jsonp',
                data: {
                    ...data,
                    zwid: this.data.options.zwid
                },
            }).then(res => {
                let newStr = app.methods.getAPIReturn(res);
                // console.log(newStr)
                if (newStr.msg != '暂无数据') {
                    var lists = newStr.data
                    var newObj = [];
                    if (lists.length >= 10) {
                        for (var i = 0; i < 10; i++) {
                            newObj.push(lists[i])
                        }
                    } else {
                        newObj = lists
                    }
                    this.setData({
                        form: false,
                        table: true,
                        chaxunjg: lists.length,
                        cxlists: newObj,
                        allLists: lists,
                        str1value: "",
                        str2value: "",
                        str3value: "",
                        str4value: "",
                        str11value: "",
                        str22value: "",
                        str33value: "",
                        xueliArrvalue: "",
                        majorvalue: "",
                        onReachBottomlists: lists,
                        onReachBottomNum: this.data.onReachBottomNum + 1
                    })
                    xhs.hideLoading()
                    xhs.showToast({
                        title: '数据查询成功!',
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    xhs.hideLoading()
                    xhs.showToast({
                        title: '未匹配到可报考岗位，请重新填写条件后查询!',
                        icon: 'none',
                        duration: 2000
                    })
                }
                resolve()
            }).catch(err => {
                console.log(err)
                resolve()
            })
        })
    },
    // 输入准考证号
    inpzkz: function (e) {
        this.setData({
            zhunkaozheng: e.detail.value
        })
        // console.log(this.data.zhunkaozheng)
    },
    // 重新检索
    changetj: function () {
        this.setData({
            table: false,
            form: true,
            allLists: "",
            cxlists: "",
            yearvalue: "",
            addressvalue: "",
            xueliArrvalue: "",
            zzmmvalue: "",
            majorvalue: "",
            str4value: "",
            str1value: "",
            str2value: "",
            str3value: "",
            majorvalue1: ""
        })
    },
    // 触底刷新
    onReachBottom: function (res) {
        if (this.data.table) {
            // 获取当前检索总页数
            if (((this.data.onReachBottomlists.length) % 10) == 0) {
                var totleCont = parseInt(this.data.onReachBottomlists.length / 10)
            } else {
                var totleCont = parseInt(this.data.onReachBottomlists.length / 10) + 1
            }
            // console.log(totleCont)
            // console.log(this.data.cxlists)
            // console.log(this.data.cxlists.length)
            // 计算当前已经展示几屏
            if (this.data.cxlists.length % 10 == 0) {
                var nowCont = parseInt(this.data.cxlists.length / 10)
            } else {
                var nowCont = parseInt(this.data.cxlists.length / 10) + 1
            }
            // console.log(nowCont)
            // console.log((this.data.onReachBottomlists.length)%10)
            // 判断是否已经全部加载完
            if ((((this.data.onReachBottomlists.length) % 10) == 0) && (totleCont == nowCont)) {
                xhs.showToast({
                    title: '我是有底线哒',
                    duration: 2000
                })
            } else if ((((this.data.onReachBottomlists.length) % 10) > 0) && (totleCont < (nowCont + 1))) {
                xhs.showToast({
                    title: '我是有底线哒',
                    duration: 2000
                })
            } else {
                // 注入新数据
                var newObj = [];
                for (var i = this.data.cxlists.length; i < (nowCont + 1) * 10; i++) {
                    if (!!this.data.allLists[i]) {
                        newObj.push(this.data.allLists[i])
                    }
                }
                xhs.showToast({
                    title: '加载完成',
                    icon: "success",
                    duration: 2000
                })
                this.setData({
                    cxlists: this.data.cxlists.concat(newObj)
                })
            }
        }
    },
    // 收藏列表
    async getcollectList(phone) {
        return new Promise((resolve, reject) => {
            xhs.request({
                url: 'https://hbapp.offcn.com/hbqt/web/index.php/Skzw/zwsearch/getcollect_list?zwid=' + this.data.options.zwid + '&callback=&',
                type: 'GET',
                dataType: 'jsonp',
                data: {
                    phone: phone
                },
                success: ((res) => {
                    var newStr = res.data.substring(1)
                    newStr = JSON.parse(newStr.substring(0, newStr.length - 1))
                    // console.log(newStr)
                    resolve(true)
                }),
                fail: ((err) => {
                    resolve(true)
                })
            })
        })
    },
    // 收藏
    collect: function (e) {
        let str1, str2, str3, str4, zwid;
        let data = {
            zwid: this.data.options.zwid,
            str1: e.currentTarget.dataset.str1,
            str2: e.currentTarget.dataset.str2,
            str3: e.currentTarget.dataset.str3,
            str4: e.currentTarget.dataset.str4,
            str5: e.currentTarget.dataset.str5,
            str6: e.currentTarget.dataset.str6,
        }
        // console.log(data)
        // xhs.request({
        //   url: 'https://hbapp.offcn.com/hbqt/web/index.php/Skzw/zwsearch/getcollect_list?zwid=' + this.data.options.zwid + '&callback=&',
        //   type: 'GET',
        //   dataType: 'jsonp',
        //   data: data,
        //   success: ((res) => {
        //     var newStr = res.data.substring(1)
        //     newStr = JSON.parse(newStr.substring(0, newStr.length - 1))
        //     console.log(newStr)
        //   }),
        //   fail: ((err) => {

        //   })
        // })
    },
    // 监听手机号
    async user_login(e){
        if(typeof e.detail == 'boolean') {
            this.setData({
                isShowAuth: e.detail
            })
        } else {
            this.setData({
                myPhone: e.detail
            })
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'));
            await this.thisMethod();
        }
    },
    // 该怎么就怎么
    thisMethod: function () {
        if (this.data.user && this.data.user.status == 1) {
            if (!this.data.user.user.gwlb && (this.data.options.lx == 'sydw' || this.data.options.lx == 'jztg')) {
                this.setData({
                    xiangmuok: true
                })
            }
            if (!this.data.user.user.zhunkaozheng && this.data.ms == 0) {
                this.setData({
                    zhunkaozhengok: true
                })
            } else {
                this.setData({
                    zhunkaozhengok: false
                })
            }
        } else {
            if (this.data.options.lx == 'sydw' || this.data.options.lx == 'jztg') {
                this.setData({
                    xiangmuok: true
                })
            }
        }
    },
    // 展开和收起
    toggle: function(e){
        const { index } = e.currentTarget.dataset;
        // console.log(index)
        let { cxlists } = this.data;
        cxlists[index].toggle = !cxlists[index].toggle
        this.setData({
            cxlists
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: this.data.k_name ? this.data.k_name : this.data.name,
            path: `/pages/zw/zw?id=${this.data.options.id}&fx=${this.data.options.fx}&gh=${this.data.options.gh}`,
            imageUrl: this.data.k_url ? this.data.k_url : '',
        }
    }
})